﻿using System;
using System.Collections.Generic;
using System.Linq;
using Games;
using Poker;

namespace Jinhua
{
    public class JiahuaGame : IGame
    {
        public JiahuaGame()
        {
            GameIndex = 0;
            MaxPokerCardCountPerPlayer = 3;
            ShuffleCount = 3;
            InitPokerCards();
        }
        public int GameIndex { get; set; }
        public int MaxPokerCardCountPerPlayer { get; set; }
        public int ShuffleCount { get; set; }
        public GameRule Rule { get; set; }
        public Stack<PokerCard> PokerCards { get; set; }
        public ICollection<Player> Players { get; set; }

        public void Play()
        {
            GameIndex++;
            Console.WriteLine("--------------------------------");
            Console.WriteLine($"第 {GameIndex} 局炸金花游戏开始!");
            ((IGame)this).Shuffle();
            ((IGame)this).Deal();
            Compare();
            ((IGame)this).Recycle();
            Console.WriteLine($"第 {GameIndex} 局炸金花游戏结束!");
            Console.WriteLine("--------------------------------");
            Console.WriteLine();
        }
        public void Compare()
        {
            Console.WriteLine("比较玩家手上的牌:");
            foreach (var player in Players)
            {
                player.DeckOfCard.Sort((x, y) => x.Suit - y.Suit);
                player.DeckOfCard.KindOfCards = Rule.GetKindOfCards(player.DeckOfCard);
                Console.WriteLine($"{player} 手中的牌是 {player.DeckOfCard} !");
            }

            var winner = Players.OrderByDescending(x => x.DeckOfCard, new DeckOfCardComparer()).FirstOrDefault();
            Console.WriteLine($"恭喜 {winner} 的手牌 {winner.DeckOfCard} 赢得第 {GameIndex} 局比赛!");
        }
        private void InitPokerCards()
        {
            PokerCards = new Stack<PokerCard>();
            foreach (var pokerCard in PokerCard.SpadesPokerCards)
            {
                PokerCards.Push(pokerCard);
            }

            foreach (var pokerCard in PokerCard.HeartsPokerCards)
            {
                PokerCards.Push(pokerCard);
            }

            foreach (var pokerCard in PokerCard.ClubsPokerCards)
            {
                PokerCards.Push(pokerCard);
            }

            foreach (var pokerCard in PokerCard.DiamondsPokerCards)
            {
                PokerCards.Push(pokerCard);
            }
        }
    }
}
